package exercises.leetcode;


import exercises.newcoder.util.ListNode;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * <a href="">
 * </a>
 *
 * <p><span color="#87CEEB">描述:</span>
 *
 * @author or2
 * @date 2021年10月31日 时间: 10:32
 */
public class No5915 {
    public int[] nodesBetweenCriticalPoints(ListNode head) {
        var res = new ArrayList<Integer>();
        var next = head.next;
        if (next.next == null) return new int[]{-1, -1};
        int pre = head.val, nextVal = next.next.val;
        int idx = 1;
        next = next.next;
        for (var p = head.next; next != null; p = next, next = next.next, idx++) {
            int curVal = p.val;
            if (curVal > pre && curVal > nextVal)
                res.add(idx);
            else if (curVal < pre && curVal < nextVal)
                res.add(idx);
            pre = curVal;
            if (next.next==null) break;
            nextVal = next.next.val;
        }

        if (res.size() < 2) return new int[]{-1, -1};
        int min = Integer.MAX_VALUE;
        for (int i = 1; i < res.size(); i++) {
            int off = res.get(i) - res.get(i - 1);
            if (off <min) min = off;
        }
        return new int[]{min, res.get(res.size() - 1) - res.get(0)};
    }

    public static void main(String[] args) {
        ListNode head = ListNode.getByIn("[5,3,1,2,5,1,2]");
        No5915 no5915 = new No5915();
        System.out.println(Arrays.toString(no5915.nodesBetweenCriticalPoints(head)));
    }
}
